package controllers

import (
	"errors"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
	"h1/models"
	"h1/utils"
	"strings"
	"time"
)

// 约定：如果子controller 存在NestPrepare()方法，就实现了该接口，
type NestPreparer interface {
	NestPrepare()
}
type baseController struct {
	beego.Controller
}
type MainController struct {
	beego.Controller
}
type IndexController struct {
	baseController
}
type ProjectController struct {
	baseController
}
type ProjectFileController struct {
	baseController
}
type ProjectSceneController struct {
	baseController
}
type SceneController struct {
	baseController
}
type SceneSceneController struct {
	baseController
}
type SceneVideoController struct {
	baseController
}
type SceneImageController struct {
	baseController
}
type SceneVersionController struct {
	baseController
}
type UserCurdController struct {
	baseController
}
type RoleController struct {
	baseController
}
type DepartmentController struct {
	baseController
}
type LoggersController struct {
	baseController
}
type SystemsThemeController struct {
	baseController
}
type PreferenceImageController struct {
	baseController
}
type ThemeController struct {
	baseController
}
type DataStatisticsController struct {
	baseController
}
type ElementCurdController struct {
	baseController
}
type SceneElementController struct {
	baseController
}
type FileUploadController struct {
	baseController
}
type FileCurdController struct {
	baseController
}
type FileDownloadController struct {
	baseController
}
type UserLoginController struct {
	baseController
}

type DataResponse struct {
	Code       int         `json:"code"`
	Msg        string      `json:"msg"`
	Data       interface{} `json:"data"`
	ServerTime string      `json:"serverTime"`
}

// 用户登录获取token
type DataResponseToken struct {
	Code       int         `json:"code"`
	Msg        string      `json:"msg"`
	Data       interface{} `json:"data"`
	Token      string      `json:"token"`
	ServerTime string      `json:"serverTime"`
}

func Response(errCode int, data interface{}, msg string) DataResponse {
	resp := DataResponse{
		Code:       errCode,
		Msg:        msg,
		Data:       data,
		ServerTime: time.Now().Format("2006-01-02 15:04:05"),
	}
	return resp
}

// 验证参数
func (this *baseController) VerifyParams(valid validation.Validation, FileEditParam interface{}) string {
	// 定义验证的结构体
	b, err := valid.Valid(FileEditParam)
	if err != nil {
		return utils.FormatErrorToString(err)
	}
	if !b {
		for _, err := range valid.Errors {
			return err.Key + err.Message
		}
	}
	return ""
}

// ReponseToken
func ResponseToken(errCode int, data interface{}, msg string, tokenstr string) DataResponseToken {
	var resp = DataResponseToken{
		Code:       errCode,
		Msg:        msg,
		Data:       data,
		Token:      tokenstr,
		ServerTime: time.Now().Format("2006-01-02 15:04:05"),
	}
	return resp
}

// -----------------------------------------
// 生成token
func (this *baseController) GenToken(uId int64, timestamp int64) (string, error) {
	return utils.GenToken(uId, timestamp)
}

// 验证token
func (this *baseController) ValidToken() (int64, bool, error) {
	authorization := strings.TrimSpace(this.Ctx.Request.Header.Get("Authorization"))
	if authorization == "" {
		return 0, false, errors.New("Authorization is empty")
	}
	if claims, isValid, err := utils.ParaseToken(authorization); err == nil && isValid {
		return claims.UId, true, nil
	}
	return 0, false, errors.New("Authorization invalid")
}

// 通过token获取用户信息
func (this *baseController) GetUserByToken() models.User {
	token := this.Ctx.Input.Header("Authorization")
	User := models.UserInfo(token)
	return User
}

// 验证参数
func (this *baseController) VerifyParas(valid validation.Validation, FileEditParam interface{}) string {
	// 定义验证的结构体
	b, err := valid.Valid(FileEditParam)
	if err != nil {
		return utils.FormatErrorToString(err)
	}
	if !b {
		for _, err := range valid.Errors {
			return err.Key + err.Message
		}
	}
	return ""
}
